<?php

namespace App\Http\Controllers\Api\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Api\Admin\BaseController;
use App\Models\Message;

class MessageController extends BaseController
{
    private $return = array(
        'code' => 1,
        'msg' => ''
    );
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Message $message,Request $request)
    {
        $map = $request->all();
        $result = $message->getDataByMap($map);
        if(!$result){
            $this->return['msg'] = '查询失败';
        }else{
            $this->return['code'] = 0;
            $this->return['data'] = $result;
        }
        return response()->json($this->return);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id,Message $message)
    {
        $data = $request->all();
        $result = $message->edit_data($id,$data);
        if ($result) {
            $this->return['code'] = 0;
            $this->return['msg'] = '修改成功';
        } else {
            $this->return['msg'] = '修改失败';
        }
        return response()->json($this->return);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id,Message $message)
    {
        $result = $message->delData($id);
        if(!$result){
            $this->return['msg'] = '删除失败！';
        }else{
            $this->return['code'] = 0;
            $this->return['msg'] = '删除成功！';
        }
        return response()->json($this->return);
    }
}
